package com.stargate.bd;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import oracle.jdbc.driver.OracleDriver;

public class ConnectionBD {

	private final static String URL_BD = "jdbc:oracle:thin:@localhost:1521:oracle";
	private final static String USER = "kantchad";
	private final static String PASSWORD = "bd2010";

	private static Connection connection;

	public static Connection getConnection() {
		if (connection == null) {
			openConnection();
		}
		return connection;
	}

	private static boolean openConnection() {
		System.out.println("*** Open connection database ***");
		try {

			// Enregistrement du driver Oracle
			System.out.print("Loading Oracle driver... ");
			DriverManager.registerDriver(new OracleDriver());
			System.out.println("loaded.");

			// Etablissement de la connection
			System.out.print("Connecting to the database... ");
			connection = DriverManager.getConnection(URL_BD, USER, PASSWORD);
			System.out.println("connected.");

			// Désactivation de l'autocommit
			connection.setAutoCommit(false);
			System.out.println("Autocommit disabled.");

		} catch (SQLException e) {
			System.err.println("failed.");
			System.err.println("SQLException in ConnectionBD.openConnection()");
			System.err.println("> " + e.getMessage());
			return false;
		}
		return true;
	}

	public static boolean closeConnection() {
		System.out.println("*** Close connection database ***");
		try {
			System.out.print("Closing the connection to the database... ");
			connection.close();
			System.out.println("closed.");
			return true;
		} catch (SQLException e) {
			System.err.println("failed.");
			System.err.println("SQLException in ConnectionBD.closeConnection()");
			System.err.println("> " + e.getMessage());
			return false;
		} catch (NullPointerException e) {
			System.err.println("failed.");
			System.err.println("Aucune ouverture de connexion à la BD.");
			return false;
		}
	}

	public static boolean commit() {
		try {
			connection.commit();
			System.out.println("*** Commit ***");
			return true;
		} catch (SQLException e) {
			if (e.getErrorCode() == 60) {
				System.err.println("*** Détection d'un interblocage. ***");
			} else if (e.getErrorCode() == 8177) {
				System.err.println("*** Détection d'un problème de sérialisation. ***");
			} else {
				System.err.println("SQLException in ConnectionBD.commit()");
				System.err.println("> " + e.getMessage());
			}
			rollback();
			return false;
		} catch (NullPointerException e) {
			System.err.println("*** Commit failed ***");
			System.err.println("Aucune ouverture de connexion à la BD.");
			return false;
		}
	}

	public static boolean rollback() {
		try {
			connection.rollback();
			System.out.println("*** Rollback ***");
			return true;
		} catch (SQLException e) {
			System.err.println("SQLException in ConnectionBD.rollback()");
			System.err.println("> " + e.getMessage());
			return false;
		} catch (NullPointerException e) {
			System.err.println("*** Rollback failed ***");
			System.err.println("Aucune ouverture de connexion à la BD.");
			return false;
		}
	}
	
	public static void setSerializable() throws SQLException {
		getConnection();
		connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
		System.out.println("*** Isolation set to SERIALIZABLE ***");
	}
	
	public static void setReadCommitted() throws SQLException {
		getConnection();
		connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		System.out.println("*** Isolation set to READ_COMMITTED ***");
	}

	public static boolean isSerializable() throws SQLException {
		try {
			return connection.getTransactionIsolation() == Connection.TRANSACTION_SERIALIZABLE;
		} catch (NullPointerException e) {
			System.err.println("*** Error in ConnectionBD.isSerializable() ***");
			System.err.println("Aucune ouverture de connexion à la BD.");
			return false;
		}
	}
	
	public static boolean isReadCommitted() throws SQLException {
		try {
			return connection.getTransactionIsolation() == Connection.TRANSACTION_READ_COMMITTED;
		} catch (NullPointerException e) {
			System.err.println("*** Error in ConnectionBD.isReadCommitted() ***");
			System.err.println("Aucune ouverture de connexion à la BD.");
			return false;
		}
	}
}
